#include "GameObject.h"

GameObject::GameObject(QObject *parent)
    : QObject(parent)
    , QGraphicsPixmapItem()
{
    // 设置初始标志
    setFlag(QGraphicsItem::ItemIsFocusable, false); // 该图元不能接收键盘焦点
    setFlag(QGraphicsItem::ItemIsMovable, false);   // 该图元不能被鼠标拖动

    // 默认使用高效渲染
    setTransformationMode(Qt::SmoothTransformation); // 图像缩放 / 旋转时启用平滑抗锯齿
}

void GameObject::setCollisionShapeMode(QGraphicsPixmapItem::ShapeMode mode)
{
    // BoundingRectShape:矩形边界
    // HeuristicMaskShape:基于像素掩码的启发式近似（动态采样）
    // MaskShape:基于像素的精确掩码
    setShapeMode(mode);
    m_useCustomShape = false;
}

void GameObject::setCustomCollisionShapeMode(const QPainterPath &path)
{
    m_customCollisionPath = path;
    m_useCustomShape = true;
}

QPainterPath GameObject::shape() const
{
    if (m_useCustomShape) {
        return m_customCollisionPath;
    }
    return QGraphicsPixmapItem::shape();
}

bool GameObject::collidesWith(const GameObject *other) const
{
    if (!other)
        return false;

    // 边界盒快速过滤,判断两个包含图元的最小边界矩形是否相交
    if (!boundingRect().intersects(other->boundingRect()))
        return false;

    return collidesWithItem(other, Qt::IntersectsItemShape);
}

void GameObject::update(float deltaTime)
{
    // 基础对象默认不执行任何更新
    Q_UNUSED(deltaTime);
}
